<!DOCTYPE html>
<html lang="ch-cn">
  <head>
　　<meta charset="utf-8">
　　<script type="text/javascript" src='../../js/jquery-3.1.1.min.js'></script>
    <style type="text/css">
    html,body, div, dl, dt, dd, ul, ol, li, h1, h2, h3, h4, h5, h6, pre, code, form, fieldset, legend, input, button, textarea, p, blockquote, th, td{
      margin: 0;
      padding:0;
    }
    *{
      -webkit-box-sizing: border-box;
      -moz-box-sizing: border-box;
      box-sizing: border-box;
    }
    ul, ol {
      margin-top: 0;
      margin-bottom: 10px;
    }
    a {
      color: #428bca;
      text-decoration: none;
    }
    .fa {
      display: inline-block;
      font-family: FontAwesome;
      font-style: normal;
      font-weight: normal;
      line-height: 1;
      -webkit-font-smoothing: antialiased;
      -moz-osx-font-smoothing: grayscale;
    }
    :before, :after {
      -webkit-box-sizing: border-box;
      -moz-box-sizing: border-box;
      box-sizing: border-box;
    }
    /*这是一个字体,可以自定义一个x图片*/
    .fa-times:before {
      content: url(../../img/button/close_tab.png);
    }

    .tabbable {
      border: none;
      margin-bottom: 10px;
    }
    .nav {
      padding-left: 0;
      margin-bottom: 0;
      list-style: none;
    }
    .nav>li {
      position: relative;
      display: block;
    }
    .nav-tabs {
      margin-bottom: 0;
      margin-left: 0;
      border: 0;
      top: 2px;
      background-color: #f3f3f3;
      -webkit-box-shadow: 0 0 4px rgba(0,0,0,.3);
      -moz-box-shadow: 0 0 4px rgba(0,0,0,.3);
      box-shadow: 0 0 4px rgba(0,0,0,.3);
      border-bottom: 1px solid #ddd;
    }
    .nav-tabs>li {
      float: left;
      margin-bottom: 0px;
      margin-left: 5px;
    }
    .nav-tabs li i {
      position: relative;
      margin-left: 10px;
      cursor: pointer;
    }
    .nav-tabs li [class*=" fa-"], .nav-tabs li [class^="fa-"] {
      width: 1.25em;
      display: inline-block;
      text-align: center;
    }
    #myTab {
      height: 40px;
      overflow: hidden;
    }
    #myTab > li {
      height: 35px;
      overflow: hidden;
      margin-top: 5px;
      background: #dedede;
      background: rgba(0,0,0,0.05);
      color: #777;
    }
    #myTab > li.active, #myTab>li:hover, #myTab>li:focus {
      background-color: #fff;
      border-bottom-color: transparent;
      box-shadow: 0 -2px 3px 0 rgba(0,0,0,.15);
    }
    #myTab > li > a {
      position: relative;
      display: inline-block;
      white-space: nowrap;
      overflow: hidden;
      padding: 8px 0px 8px 10px;
      margin: 0 20px 0 0;
      border-top: 0px;
      box-shadow: none;
      background: transparent;
      line-height: 17px;
      border: 0;
      max-width: 108px;
      color: #777;
    }fa fa-times
    #myTab > li.active {
      border-top: 2px solid #2dc3e8;
    }
    #myTab li [class*=" fa-"], #myTab li [class^="fa-"] {
      vertical-align: middle;
      margin-left: 0px;
      position: absolute;
      right: 0px;
      margin-top: 10px;
    }
    .tab-content {
      background: none;
      padding: 0;
      padding-top: 5px;
      position: relative;
    }
    .tab-content > div{
      display: none;
    }
    .tab-content > div.active{
      display: block;
    }
    </style>
  </head>
  <body>
  
    <div class="tabbable" id="tabs" style="border:none;">
      <!-- 页面标签列表 -->
      <ul class="nav nav-tabs" id="myTab">
      </ul>
      <!-- 页面内容列表，和页面标签列表对应 -->
      <div class="tab-content">
      </div>
    </div>
  </body>
  <script type="text/javascript">

  //切换tab页的显示
  $(document).on('click','#myTab > li',function(e){
    //清除原来显示的tab页
    var oldTab = $("#myTab li.active").removeClass("active").find("a[data-toggle='tab']");
    $(oldTab.attr("href")).removeClass("active");

    //设置新的显示tab页
    var newTab = $(this).addClass("active").find("a[data-toggle='tab']");
    $(newTab.attr("href")).addClass("active");

    refreshTabHistory(false/*isDelete*/,$(this).attr('id').substring(4));
  })
  //手动调用切换到要显示的tab页,当前的action只支持show
  //eg:$("#tab-0 a[data-toggle='tab']").tab("show");
  $.fn.tab = function(action){
    if(action == "show"){
      $(this).parent().click();
    }
  }

  var currentTabId = '';//当前焦点Tab
  //在非左侧菜单栏弹出的tab页也会用到该数据，如common.js中的pageForward函数
  var pageCounter = 0;
  /*
  id:       tab页签的html标签ID属性格式为"tab-"+id，内容容器的html标签ID格式为"tab-content-"+id
  text:     tab页签的显示文本
  url:      打开的iframe的url
  innerTab: 是否是内部弹出页（打开的tab页触发添加新的tab页），默认为undefined/false
  */
  function addTab(id,text,url,innerTab) {
    //如果某个页面已经打开，则切换到该页显示即可，不会新添加tab页
    if($('#myTab #tab-'+id).length > 0){
      $('#myTab  #tab-' + id + ' a').tab('show');
    }else{
      var tab_id = "tab-" + id,
      tab_content_id = "tab-content-"+id;
      
      //添加tab页签
      $("#myTab > li").removeClass("active");
      $("#myTab").append("<li id='" + tab_id + "' class='active'><a data-toggle='tab' href='#"
        + tab_content_id + "'>" + text + "</a>"
        + ("<i class='fa fa-times' onclick='deleteTab(\"" + id + "\")'></i>") + "</li>");
      
      //添加新的内容显示
      $(".tab-content > div").removeClass("active");
      $(".tab-content").append("<div id='"+ tab_content_id +"' class='active' style='height:100%;'>"
        + "<iframe id='iframepage" + (pageCounter++) + "' name='iframepage" + (pageCounter++) 
        + "' width='100%' height='100%' frameborder='0' scrolling='no'  src='" + url + "'></iframe></div>");      
    }
    //刷新切换tab的历史记录
    refreshTabHistory(false/*isDelete*/,id);
    //重新设置tab页签的宽度
    refreshWidth();
  }
  //参数id为tab的标志，但是并不是tab页的id属性，真正的id属性值是"tab-"+id
  function deleteTab(id){
    var tabJQ = $("#tab-"+id),
    tabContentJQ = $("#tab-content-" + id);    
    
    if(!tabJQ.hasClass("active")){
      tabJQ.remove();
      tabContentJQ.remove();
      refreshTabHistory(true/*isDelete*/,id);
    }else{    
      tabJQ.remove();
      tabContentJQ.remove();
      refreshTabHistory(true/*isDelete*/,id);
      $('#tab-' + currentTabId + ' > a').tab('show').click();
    }
    refreshWidth();
  }
  //关闭当前tab页的快速方法
  function closeCurrentTab(){
    deleteTab(currentTabId);
  }

  /*
  刷新页签切换历史
  isdelete: 是否是删除tab页签,true:是，false：否
  curTabId：要处理的tab页签的id,tab页签html标签元素的ID属性格式为"tab-"+curTabId
  */
  function refreshTabHistory(isdelete,curTabId){
    if(!refreshTabHistory.histoty){
      //用来记录用户点击tab的历史
      refreshTabHistory.histoty = [];
    }
    var index = 0,
    leng = refreshTabHistory.histoty.length;
    //查找传入的tab页签在历史记录中的位置
    for(; index < leng; index++){
      if(refreshTabHistory.histoty[index] == curTabId){
        break;
      }
    }
    
    //如果是删除页签，直接在历史记录中删除即可，历史记录的其他页签的顺序不变
    if(isdelete){
      refreshTabHistory.histoty.splice(index,1);

    //如果是新增页签，先保证历史记录中没有改页签（有就删掉），然后将新增的页签放在历史记录的最后面（即该页签为最新）
    }else{
      if(index < leng) {
        refreshTabHistory.histoty.splice(index,1);
      }
      refreshTabHistory.histoty.push(curTabId);
    }
    currentTabId = refreshTabHistory.histoty[refreshTabHistory.histoty.length - 1];
  }

  //刷新重置tab页签的宽度
  function refreshWidth(){
    var panelWidth = $('#myTab').width() - 20/*可能存在的滚动条宽度*/,
    tabs = $('#myTab > li'),
    tabContentAverageWidth = 0/*tab > a标签的宽度*/,
    minTabAverageWidth = 25/*margin-left:5,X按钮宽度为20*/,
    zeroContentTabWidth = 35/*当tab > a标签宽度为0时tab标签对应的宽度是30px，外加上margin-left:5*/,
    aPaddingLeft = 10/*tab > a标签的padding-left默认是10，当averageWidth< 35需要调整*/;
    
    averageWidth = parseInt(panelWidth/(tabs.length),10);// 
    if(averageWidth >= zeroContentTabWidth){
      tabContentAverageWidth = averageWidth - zeroContentTabWidth;
      
    /*35 > averageWidth >= 25*/ 
    }else if(averageWidth >= minTabAverageWidth){
      tabContentAverageWidth = 0;
      aPaddingLeft = averageWidth - minTabAverageWidth;
      
    //averageWidth < 25
    }else{
      tabContentAverageWidth = 0;
      aPaddingLeft = 0;
    }
    //tab页签名称元素a标签的宽度和padding-left。这个是在box-sizing:border-box。的情况下
    tabs.find('>a').css({'width':(tabContentAverageWidth + aPaddingLeft),'padding-left':aPaddingLeft});
  }

  	var h = document.documentElement.clientHeight;
		$(".tab-content").css("height", h-80);
  </script>  
</html>